//
//  TLUserManger.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/8/24.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "TLUserManger.h"


@implementation TLUserManger
@synthesize avUM;


- (id)init{
    self = [super init];
    
    if(self)
    {
        
    }
    
    return self;
}

+ (TLUserManger *)sharedManger{
    static dispatch_once_t onceToken;
    static TLUserManger    *userManger;
    dispatch_once(&onceToken, ^{
        userManger = [[TLUserManger alloc] init];
        [userManger setAvUM:[AVOSUserManger sharedManger]];
    });
    
    return userManger;
}

- (void)setAVUser:(AVUser *)user{
    [self.avUM setLoginUser:user];
}

- (UIImage *)getUserHead{
    return [[AVOSUserManger sharedManger] getUserFace];
}
/**
 *  Get User Infos From the AVOS Server
 *
 *  email ,session token , username , mobile phone, user Face ,is mobile phone verfied
 *  @return A Dictionary contains the values top
 */
- (NSDictionary *)getUserInfos{
    NSDictionary *userInfo = nil;
    @try {
      userInfo =  @{@"username" : [[AVOSUserManger sharedManger] getUserName], @"session" : [[AVOSUserManger sharedManger] getSession]};
    }
    @catch (NSException *exception) {
        NSLog(@"Has nil value in userinfos %@",[exception description]);
        userInfo = @{@"username" : @"不存在", @"session" : @"不存在"};
    }
    @finally {

    }
    
    
    return userInfo;
}

#pragma mark -- Property set&get
- (void)setAvUM:(AVOSUserManger *)avUM_{
    avUM = avUM_;
}

#pragma mark -- User Login Action
- (BOOL)hasUser{
    return [[AVUser currentUser] isAuthenticated];
}

- (void)loginWithSession:(NSString *)sessionToken loginCompletion:(void(^)(BOOL loginSucc))completion{
    [AVUser becomeWithSessionTokenInBackground:sessionToken block:^(AVUser *user, NSError *error) {

        BOOL loginSucc = user ? YES:NO;
        
        completion(loginSucc);
    }];
}

- (void)logout{
    [AVUser logOut];
}
@end

